package com.leve.control.mapper;

import com.leve.control.domain.BackUser;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author whongyu
 * @create by 2019/6/12
 */
@Repository
public interface BackUserMapper {

    @Select("<script>" +
            " select `user_id`,`username`,`password`,`createtime`,`permission` from ims_levepower_iot_backuser " +
            "<where>  " +
            "<if test=\"userId != null and userId != ''\"> and user_id = #{userId} </if>" +
            "<if test=\"userName != null and userName != ''\"> and username = #{userName} </if>" +
            "<if test=\"password != null and password != ''\"> and password = #{password} </if>" +
            "<if test=\"createTime != null and createTime != ''\"> and createtime = #{createTime} </if>" +
            "<if test=\"permission != null and permission != ''\"> and permission = #{permission} </if>" +
            "</where>" +
            "<choose>" +
            "     <when test=\"sort != null and sort.trim() != ''\">" +
            "         order by ${sort} ${order}" +
            "     </when>" +
            "     <otherwise>" +
            "        order by user_id desc" +
            "     </otherwise>" +
            "</choose>" +
            "</script>")
    @ResultType(BackUser.class)
    List<BackUser> findUser(Map<String,Object> map);

    @Select("select `user_id`,`username`,`password`,`createtime`,`permission` from ims_levepower_iot_backuser where user_id = #{userId}")
    BackUser findById(int userId);

    @Insert("insert into ims_levepower_iot_backuser(`user_id`, `username`, `password`, `createtime`, `permission`) values(#{userId}, #{userName}, #{password}, #{createTime}, #{permission})")
    int addBackUser(BackUser backUser);

    @Select("delete from ims_levepower_iot_backuser where user_id = #{userId}")
    int remove(int userId);

    @Update("<script>" +
            "update ims_levepower_iot_backuser " +
            "<set>" +
            "<if test=\"userName != null\">`username` = #{userName}, </if>" +
            "<if test=\"password != null\">`password` = #{password}, </if>" +
            "<if test=\"createTime != null\">`createtime` = #{createTime}, </if>" +
            "<if test=\"permission != null\">`permission` = #{permission} </if>" +
            "</set>" +
            " where user_id = #{userId}" +
            "</script>")
    int update(BackUser backUser);
}
